'use strict';
/**
    自定义报表    
*/
var reportTotle = angular.module('reportTotle', ['tm.pagination', 'Common', 'knowledgeSelector', 'PensonSelector', 'knowledgebaseS', "btnconqitk"])
.controller('reportTotleController', ['$scope', "$rootScope", "$timeout",'$interval', 'RequestTrainService', '$state', 'Msg', 'MsgCon', '$q', 'requestStatisticService', 'sysUserInfo', 'T', function ($scope, $rootScope,$timeout, $interval, RTS, $state, Msg, MsgCon, $q, RS, sysUserInfo, T) {

    $scope.btnlist = { addinfo: false, editinfo: false, delinfo: false };
    $rootScope.btnTab = "index.Train.ReportForm";

    $scope.list=[
        {
            title:"培训统计",
            list:[
                {name:"课程学习记录导出",type:"课程",no:"1",icon:"&#xe756;",desc:"统计单个课程学员的学习记录",color:"#ff9976",color2:"#ff653b",loading:false},
                {name:"试卷考试成绩导出",type:"考试",no:"2",icon:"&#xe631;",desc:"统计单张试卷学员的考试成绩",color:"#39d983",color2:"#ff653b",loading:false},
                {name:"任务学习记录导出",type:"任务",no:"3",icon:"&#xe61c;",desc:"统计单个任务学员的学习进度",color:"#ffcf82",color2:"#ffb740",loading:false}
            ]
        },
        {
            title:"其他统计",
            list:[
                {name:"系统人员",type:"其他",no:"9",icon:"&#xe64d;",desc:"导出系统人员列表",color:"#ff9976",color2:"#ff653b",loading:false},
                {name:"平台月活访问量",type:"其他",no:"10",icon:"&#xe6e6;",desc:"导出一月内平台活跃度报表",color:"#ffcf82",color2:"#ffb740",loading:false}
            ]
        }
        
    ];
    


    $scope.GoReport=function(){
        $state.go("index.Train.customReport");
    }


    // 打开人员选择器
    $scope.openUserInfo = function (event) {
        $scope.userInfoConfig.ApiOpen = true;
        $scope.userInfoConfig.event = event;
    }
    // 初始化知识分类
    $scope.knowConfig = {   // 配置知识选择器的参数
        menu: [], //["courseware","examination","trainingTask","class"],
        windowSize: "standard", //  窗口类型 standard:标准(默认)  mini :迷你          
        kscMore: false, // 课程列表是否多选
        kscList: [], // 课程选中列表
        kseMore: false, // 试卷列表是否多选
        kseList: [], // 试卷选中列表 
        ksrMore: false, // 任务是否多选
        ksrList: [], // 任务列表
        isShow: false,  // 显示知识选择器
        classMore:false,//班级 是否多选
        classList:[],//班级列表
        callbackParams: {   //回调操作型参数 选择填写参数   配合回调方法使用
            isClose: true
        }
    };

    //日志
    $scope.logList=[];

    $scope.showLog=false;

    //打开
    $scope.showForm=function(){
        $scope.showLog=!$scope.showLog;
    }

    $scope.showTips=function(item){
        if(item.loading){
            MsgCon.info("操作提示","上一个报表正在导出，请导出完成后再试！",{},"确定","")
            return;
        }
        $scope.nowItem=item;

        if(item.no<6){
            MsgCon.info("操作提示","您需要先选择一个"+item.type+"后才可以导出数据！",{},"确定","取消").then(function (flag) { 
                if (flag) {
                    
                // $scope.knowConfig.menu //["courseware","examination","trainingTask","class"],
                    if(item.no=="1"){
                        $scope.knowConfig.menu=["courseware"];
                        $scope.knowConfig.kscList=[];
                    }else if(item.no=="2"){
                        $scope.knowConfig.menu=["examination"];
                        $scope.knowConfig.kseList=[];
                    }else if(item.no=="3"){
                        $scope.knowConfig.menu=["trainingTask"];
                        $scope.knowConfig.ksrList=[];
                    }else if(item.no=="4"||item.no=="5"){
                        $scope.knowConfig.menu=["class"];
                        $scope.knowConfig.classList=[];
                    }

                    $scope.knowConfig.isShow=true;
                }
            });
        }else{
            MsgCon.info("操作提示","确定导出"+item.name+"数据吗？",{},"确定","取消").then(function (flag) { 
                if (flag) {

                    var obj=angular.copy($scope.nowItem);
                    obj.date=$scope.getCurrentDate();
                    obj.id=new Date().getTime();
                    obj.state=1;//进行中
                    $scope.nowItem.loading=true;
                    obj.typename="-";
                    $scope.logList.push(obj);
                    $scope.showForm();
                    if(item.no==6){

                        $scope.exportClassAddress(obj.id);

                    }else if(item.no==7){

                        $scope.exportClassPlanComplete(obj.id);

                    }else if(item.no==8){

                        $scope.exportClassNoPlan(obj.id);

                    }else if(item.no==9){

                        $scope.exportUserList(obj.id);
                        
                    }else if(item.no==10){
                        $scope.exportActivity(obj.id);
                    }
                }
            });

        }
    }

    //A标签导出链接
    $scope.reportUrl="";

    //选择器回调
    $scope.onCallback=function(){

        //未选择任务数据
        if($scope.knowConfig.kscList.length<=0&&$scope.knowConfig.kseList.length<=0&&$scope.knowConfig.ksrList.length<=0&&$scope.knowConfig.classList.length<=0){
            return;
        }

        $scope.showForm();

        var obj=angular.copy($scope.nowItem);
        obj.date=$scope.getCurrentDate();
        obj.id=new Date().getTime();
        obj.state=1;//进行中
        $scope.logList.push(obj);

        //课程导出
        if($scope.nowItem.no=="1"&&$scope.knowConfig.kscList.length>0){

            $scope.nowItem.loading=true;
            $scope.reportUrl=$rootScope.managerurl+"/course/courseExport?courseid="+$scope.knowConfig.kscList[0].course_Id+"&coursename="+$scope.knowConfig.kscList[0].course_Name;
            
            obj.typename=$scope.knowConfig.kscList[0].course_Name;
            $timeout(function(){
                document.getElementById("reportUrlHref").click();
            },50);
            $scope.updateState(obj.id,2);

        //试卷
        }else if($scope.nowItem.no=="2"&&$scope.knowConfig.kseList.length>0){

            $scope.nowItem.loading=true;
            obj.typename=$scope.knowConfig.kseList[0].paperName;
            $scope.exportPaperExcel(obj.id);

        //任务
        }else if($scope.nowItem.no=="3"&&$scope.knowConfig.ksrList.length>0){

            $scope.nowItem.loading=true;
            obj.typename=$scope.knowConfig.ksrList[0].name;
            $scope.exportArrangeExcel(obj.id);

        //班级名册
        }else if($scope.nowItem.no=="4"&&$scope.knowConfig.classList.length>0){

            $scope.nowItem.loading=true;
            obj.typename=$scope.knowConfig.classList[0].gradeName;
            $scope.exportClassStudent(obj.id);

        //班级考勤
        }else if($scope.nowItem.no=="5"&&$scope.knowConfig.classList.length>0){

            $scope.nowItem.loading=true;
            obj.typename=$scope.knowConfig.classList[0].gradeName;
            $scope.exportClassKaoqin(obj.id);
        }
    }

    $scope.getCurrentDate=function () {
        var now = new Date();
        var year = now.getFullYear(); //得到年份
        var month = now.getMonth();//得到月份
        var date = now.getDate();//得到日期
        var day = now.getDay();//得到周几
        var hour = now.getHours();//得到小时
        var minu = now.getMinutes();//得到分钟

        month = month + 1;
        if (month < 10) month = "0" + month;
        if (date < 10) date = "0" + date;
        if (hour < 10) hour = "0" + hour;
        if (minu < 10) minu = "0" + minu;
        var time = year + "-" + month + "-" + date+ " " + hour + ":" + minu;

        return time;
    }
    
    //修改日志状态
    $scope.updateState=function(id,state){
        $scope.logList.forEach(function(item,index){
            if(item.id==id){
                item.state=state;
                $scope.list.forEach(function(obj,cindex){
                    for(var a=0;a<obj.list.length;a++){
                        if(item.no==obj.list[a].no){
                            obj.list[a].loading=false;
                            break;
                        }
                    }
                    
                })
            }
        });
    }
    //试卷成绩导出
    $scope.exportPaperExcel = function (id) {
        var paramExport = {
            paperid: $scope.knowConfig.kseList[0].paperId,
            orgid: sysUserInfo.getuserinfo().organization_ID,
            orderby: "desc",
            correct: "3",
            single: "2", //全部  1 单人最高传2
            sort: "1",
            export: "2"
        };
        RTS.paperStatistics(paramExport).then(function (response) {
            if (response.errorcode == "0" && response.errormsg != undefined && response.errormsg.length > 0) {
                $scope.updateState(id,2)
                window.location = response.errormsg;
            }
            else if (response.errorcode == "73" || (response.errorcode == "0" && (response.errormsg == undefined || response.errormsg.length <= 0))) {
                setTimeout(function () {
                    $scope.exportPaperExcel(obj);
                }, 3000);
            } else {
                $scope.updateState(id,3)
            }
        }, function errorCallback() {
            $scope.updateState(id,3)
        });

    }

    //请求任务导出
    $scope.exportArrangeExcel=function(id){
        var param={
            arrangeid: $scope.knowConfig.ksrList[0].id,
            orgId:sysUserInfo.getuserinfo().organization_ID,
            arrangeName:$scope.knowConfig.ksrList[0].name,
            isPublic:$scope.knowConfig.ksrList[0].is_public, 
            course_count: $scope.knowConfig.ksrList[0].courser_count
        };
        RTS.taskExport(param).then(function(data){                          
            if (data.errorcode == "0") {
                $scope.updateState(id,2)
                window.location = data.errormsg;
            }
            else if (data.errorcode == "73") {
                setTimeout(function () {
                    $scope.exportArrangeExcel(id);
                }, 5000);
            } else {
                $scope.updateState(id,3)
            }
        }, function callback() {                     
            $scope.updateState(id,3) 
        });    
    }

    //班级名称
    $scope.exportClassStudent=function(id){
        var param={
            gradeid: $scope.knowConfig.classList[0].gradeId,
            state: 2
        }
        //查询学员信息与考勤信息
        RTS.findStudentList2(param).then(function (data) {
            if (data.errorcode == "0") {
                $scope.updateState(id,2)
                window.location = data.errormsg;
            }
            else if (data.errorcode == "73") {
                setTimeout(function () {
                    $scope.exportClassStudent(id);
                }, 5000);
            } else {
                $scope.updateState(id,3)
            }
        }, function callback() {                     
            $scope.updateState(id,3)
        });  
     }
    //班级考勤
    $scope.exportClassKaoqin=function(id){
        var param={
            gradeId:$scope.knowConfig.classList[0].gradeId,
            orgId:sysUserInfo.getuserinfo().organization_ID,
            export:2
        }
        //查询学员信息与考勤信息
        RTS.GetKaoqinList(param).then(function (data) {
            if (data.errorcode == "0") {
                $scope.updateState(id,2)
                window.location = data.errormsg;
            }
            else if (data.errorcode == "73") {
                setTimeout(function () {
                    $scope.exportClassKaoqin(id);
                }, 5000);
            } else {
                $scope.updateState(id,3)
            }
        }, function callback() {                     
            $scope.updateState(id,3) 
        });  
     }


    //培训基地导出
    $scope.exportClassAddress=function(id){
        var param={
            userid: sysUserInfo.getuserinfo().user_ID,  //登录人id
            orgid: sysUserInfo.getuserinfo().organization_ID, //登录人企业id
            powerLV: sysUserInfo.getuserinfo().powerLV,
            state:2
        }
        RTS.findTrainbase(param).then(function (data) {
            if (data.errorcode == "0") {
                $scope.updateState(id,2)
                window.location = data.errormsg;
            }
            else if (data.errorcode == "73") {
                setTimeout(function () {
                    $scope.exportClassAddress(id);
                }, 5000);
            } else {
                $scope.updateState(id,3)
            }
        }, function callback() {                     
            $scope.updateState(id,3)  
        });  
     }


    //计划完成度
    $scope.exportClassPlanComplete=function(id){
        var param={
            userid: sysUserInfo.getuserinfo().user_ID,  //登录人id
            orgid: sysUserInfo.getuserinfo().organization_ID, //登录人企业id
            powerLV: sysUserInfo.getuserinfo().powerLV,
            state:2
        }
        RTS.biddingUnitTable(param).then(function (data) {
            if (data.errorcode == "0") {
                $scope.updateState(id,2)
                window.location = data.errormsg;
            }
            else if (data.errorcode == "73") {
                setTimeout(function () {
                    $scope.exportClassPlanComplete(id);
                }, 5000);
            } else {
                $scope.updateState(id,3)
            }
        }, function callback() {                     
            $scope.updateState(id,3)  
        });  
     }

    //非计划内开班
    $scope.exportClassNoPlan=function(id){
        var param={
            type:2,
            uid: sysUserInfo.getuserinfo().user_ID  //登录人id
 
        }
        RTS.findGradeLog(param).then(function (data) {
            if (data.errorcode == "0") {
                $scope.updateState(id,2)
                window.location = data.errormsg;
            }
            else if (data.errorcode == "73") {
                setTimeout(function () {
                    $scope.exportClassNoPlan(id);
                }, 5000);
            } else {
                $scope.updateState(id,3)
            }
        }, function callback() {                     
            $scope.updateState(id,3)  
        });  
     }


    //人员
    $scope.exportUserList=function(id){
        var param={
            stype:1, 
            organization_ID:sysUserInfo.getuserinfo().organization_ID,
            userId:sysUserInfo.getuserinfo().user_ID,
            powerLV:sysUserInfo.getuserinfo().powerLV,
            type:2
        }
        RTS.personnelGetList(param).then(function (data) {
            if (data.errorcode == "0") {
                $scope.updateState(id,2)
                window.location = data.errormsg;
            }
            else if (data.errorcode == "73") {
                setTimeout(function () {
                    $scope.exportUserList(id);
                }, 5000);
            } else {
                $scope.updateState(id,3)
            }
        }, function callback() {                     
            $scope.updateState(id,3)  
        });  
     }

    //运营
    $scope.exportActivity=function(id){
        var param={
            organization_ID:sysUserInfo.getuserinfo().organization_ID,
            pageSize: 5,
            powerLV: 2,
            searchOrgIds:"", 
            stateTime:"",
            type: 2,
            user_ID: sysUserInfo.getuserinfo().user_ID
        }
        RTS.findStatisticsRankByProActivity(param).then(function (data) {
            if (data.errorcode == "0") {
                $scope.updateState(id,2)
                window.location = data.errormsg;
            }
            else if (data.errorcode == "73") {
                setTimeout(function () {
                    $scope.exportActivity(id);
                }, 5000);
            } else {
                $scope.updateState(id,3)
            }
        }, function callback() {                     
            $scope.updateState(id,3)  
        });  
     }




} ])
